/**
 * Created by lupf on 2016/4/28.
 */

angular.module('myApp')

    .controller('queryCtrl',['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'limitToFilter', 'filterFilter', 'dateFilter', 'myConstant', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                       CacheService, limitToFilter, filterFilter, dateFilter, myConstant) {
        //视图层变量viewModel
        var vm = $scope.vm = {};
        vm.constant = myConstant;
        //变量初始化
        //分页数据
        vm.pagination = {
            pageSize: 10,
            pageNum: 1
        };
        //查询条件
        vm.queryBean = {};

        //缓存数据初始化(需要缓存的key请自定义)
        /*数据格式{
         key1 :value1,
         key2:value2
         }*/
        vm.cached = {
            MERCHANT_CODE: {},
            SPECIALLY_AUDIT_STATUS:{}
        };

        (function initCache() {

            CacheService.getObj('MCC_CODE', function (key, cacheObj) {
                vm.cached['MCC_CODE'] = cacheObj;
            });

            CacheService.initCaches(vm.cached);
        }());

        /*将前面缓存的数据格式化
         [{key:value},{key:value}]*/
        vm.getCache = function (key) {
            return CacheService.getCache(key);
        };

            //特约商户数据字典
            vm.cache = {
                MERCHANT_PROPERTY: {},
                MERCHANT_TYPE: {},
                LEGAL_CREDENTIALS_TYPE: {}
            };
//批量获取缓存
            var keys = [];
            for (var key in  vm.cache) {
                keys.push(key);
            }
            $log.debug('批量查询缓存');
            $http.post(config.ctx + '/specially/dataDic/cache/get', keys, config.jsonHeader)
                .then(function (response) {
                    var tempMap = response.data.object;
                    for (var key in vm.cache) {
                        if (!angular.isUndefined(tempMap[key])) {
                            vm.cache[key] = tempMap[key];
                        }
                    }
                }, function (response) {
                    $log.error('获取数据%s失败', keys);
                });





            //获取对象数据[user,user,user]
        vm.getObj = function (key, input) {
            return $http.post(config.ctx + '/base/cache/getAll',
                $httpParamSerializerJQLike({cacheKey: key}))
                .then(function (response) {
                    return limitToFilter(filterFilter(response.data.object, input), 6);
                }, function (response) {
                    $log.error('获取数据%s失败', cacheKey);
                })
        };

        vm.queryDetail = queryDetail;
        vm.resetForm = resetForm;

        /*------------------以上配置通用---------------------*/


        vm.uploadItem = uploadItem;
        vm.uploadDetailDelete = uploadDetailDelete;

        /*------------------以上方法名可选择性通用---------------------*/

        /*CacheService.initCache(vm.cached, function (cacheKey, cacheObj) {
         $log.debug(cacheKey, cacheObj);
         vm.cached[cacheKey] = cacheObj;
         });*/

        /*vm.getCache = function (key) {
         CacheService.getCache(key)
         }*/

        function queryDetail() {
            var queryForm = document.getElementById('queryForm');
            vm.url = angular.element(queryForm).prop('action');

            //组织参数：查询条件 + 分页数据
            var postData = angular.copy(vm.queryBean);
            postData.pageSize = vm.pagination.pageSize;
            postData.pageNum = vm.pagination.pageNum;

            $http.post(vm.url, $httpParamSerializerJQLike(postData))
                .then(function (response) {
                    var data = response.data;
                    if (data.success) {
                        vm.pagination = data.object;
                        //清空选中记录
                        CheckboxService.clearChecked();
                    } else {
                        alert(data.msg);
                    }
                });

        };

        function resetForm() {
            vm.queryBean = {};
        }


        function uploadItem() {

            OpenService({
                modalTitle: '编辑上报',
                modalBody: 'toDelete',
                url: 'upload',

                cached: {
                    LEGAL_CREDENTIALS_TYPE:vm.cache.LEGAL_CREDENTIALS_TYPE,
                    MERCHANT_TYPE:vm.cache.MERCHANT_TYPE
                }
            }, function (item) {
                vm.queryDetail();
            },'updateMerchantInfoDeleteModalInstanceCtrl','myModalNoFooter.html');
        }

        function uploadDetailDelete(bean) {
            var item = angular.copy(bean);
            console.info(item);
            OpenService({
                modalTitle: '查看详情',
                modalBody: 'toDetail',
                url: '#',
                item: item,
                cached: {
                    LEGAL_CREDENTIALS_TYPE:vm.cache.LEGAL_CREDENTIALS_TYPE,
                    MERCHANT_TYPE:vm.cache.MERCHANT_TYPE,
                    SPECIALLY_AUDIT_STATUS:vm.cached.SPECIALLY_AUDIT_STATUS
                }
            }, function (item) {
            },'','myModalNoSave.html');

        }

    }]);


angular.module('myApp').controller('updateMerchantInfoDeleteModalInstanceCtrl',['$scope', '$uibModalInstance', 'modalItem', '$http',
    '$httpParamSerializerJQLike', '$log', 'dateFilter', 'myConstant', function ($scope, $uibModalInstance, modalItem, $http,
                                                                                    $httpParamSerializerJQLike, $log, dateFilter, myConstant) {

    var vm = $scope.vm = {
        modalTitle: 'please change the title',
        modalBody: '#',
        url: '',
        item: {}
    };

    console.log(modalItem);
    vm = $scope.vm = modalItem;
    vm.constant = myConstant;

        $scope.save = function () {
            var postData = angular.copy(vm.item);
            delete postData.pageNum;
            delete postData.pageSize;
            delete postData.rowCount;
            delete postData.$$hashKey;
            $log.debug(postData);
            $http.post(vm.url, postData, config.jsonHeader)
                .then(function (response) {
                    if (response.data.success) {
                        alert("操作成功");
                        $uibModalInstance.close(vm.item);
                    } else {
                        alert(response.data.msg);
                    }
                }, function (response) {
                    alert("操作失败");
                    alert(response.status);
                });

        };
        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
}]);